$(function () {
    local()

    let user = JSON.parse(localStorage.getItem('user'))
    let first = `
    <li class="mui-table-view-cell mui-media">
        <a class="mui-navigate-right" href="#account">
            <img class="mui-media-object mui-pull-left head-img" id="head-img" src="">
            <div class="mui-media-body">
                ${user.mobile}
                <p class='mui-ellipsis'>账号:${user.username}</p>
            </div>
        </a>
    </li>           
    `
    $('#first').html(first)

    let second = `
    
        <ul class="mui-table-view">
            <li class="mui-table-view-cell">
                <a id="head" class="mui-navigate-right">头像
                    <span class="mui-pull-right head">
                        <img class="head-img mui-action-preview" id="head-img1" src="" />
                    </span>
                </a>
            </li>
            <li class="mui-table-view-cell">
                <a>姓名<span class="mui-pull-right">${user.username}</span></a>
            </li>
            <li class="mui-table-view-cell">
                <a>QQ号<span class="mui-pull-right"></span></a>
            </li>
            <li class="mui-table-view-cell">
                <a>手机号<span class="mui-pull-right">${user.mobile}</span></a>
            </li>
            <li class="mui-table-view-cell">
                <a>邮箱地址<span class="mui-pull-right"></span></a>
            </li>
            <li class="mui-table-view-cell" style="text-align:center;background-color:red;">
                修改信息需联系管理员
            </li>
        </ul>
    `
    $('#second').html(second)

    //退出登陆
    $('#logout').on('tap', () => {
        localStorage.removeItem('user')
        $.ajax({
            type: 'GET',
            url: '/user/logout',
            success: (result) => {
                if (result) {
                    mui.alert('提出成功!', () => {
                        window.location.replace('/login')
                    });
                    return
                } else {
                    mui.alert('无法访问服务,请联系wx:13278865643');
                }
            }
        })
    })

    //修改密码
    $('#updatePwd').on('tap', () => {
        let oldPassword = $.trim($('#oldPassword').val())
        let newPassword = $.trim($('#newPassword').val())
        let newPassword_confirm = $.trim($('#newPassword_confirm').val())
        if (!oldPassword) {
            mui.toast('旧密码不能为空!');
            return
        }
        if (!newPassword) {
            mui.toast('新密码不能为空!');
            return
        }
        if (!newPassword_confirm) {
            mui.toast('确认密码不能为空!');
            return
        }
        if (newPassword !== newPassword_confirm) {
            mui.toast('密码与确认密码不相同!');
            return
        }
        $.ajax({
            type: 'POST',
            url: '/user/updatePassword',
            dataType: 'json',
            data: {
                oldPassword, newPassword
            },
            success: (result) => {
                console.log(result)
                if (result.error) {
                    mui.toast(result.message);
                    return
                } else {
                    mui.alert('修改成功!', () => {
                        window.location.replace('/setting')
                    });
                }
            }
        })
    })

    //获取所有收获地址
    $('#addr').on('tap', () => {
        getAddresses()
    })

    //加载购物车列表
    uploadCar()

    //商品删除功能
    $(document).on('tap', '#delCar', (e) => {
        delCar(e.currentTarget.dataset.id)
    })
    //清空购物车
    $(document).on('tap', '#clearCars', () => {
        clearCar()
    })
})

//修改的地址的方法集合(改和查)
function getAddresses() {
    $.ajax({
        type: 'GET',
        url: '/address/queryAddress',
        success: (result) => {
            if (!result) {
                mui.alert('无法访问服务,请联系wx:13278865643', () => {
                    return
                });

            }
            let str = `<li class="mui-table-view-cell" style="text-align: center;">
                            <a href="#addAddress" class="mui-navigate-center">添加收货地址 +</a>
                        </li>
                        `
            for (let r in result) {
                let addr = result[r]
                str += `
                <li class="mui-table-view-cell">
                    <div class="mui-slider-handle">
                        <a class="mui-icon mui-icon-forward mui-pull-right"></a>
                        <a href="#updateAddr"  class="updateGO"  
                            data-id="${addr.id}" 
                            data-address="${addr.address}"
                            data-addressDetail="${addr.addressDetail}"
                            data-recipients="${addr.recipients}"
                            data-postcode="${addr.postCode}">
                        <div class="addressList"   data-id="${addr}">
                            <span>邮编:${addr.postCode}</span>&nbsp;&nbsp;&nbsp;&nbsp;<span>收货人:${addr.recipients}</span>
                            <p>${addr.address}${addr.addressDetail}</p>
                        </div>
                        </a>
                    </div>
                    <div class="mui-slider-right mui-disabled">
                        <a id="delAddr" class="mui-btn mui-btn-red" data-id="${addr.id}">删除</a>
                    </div>
                </li>
                `
            }



            $('#addrs').html(str)

            //删除收货地址
            $(document).on('tap', '#delAddr', (e) => {
                $.ajax({
                    type: 'POST',
                    url: '/address/deleteAddress',
                    data: {
                        id: e.currentTarget.dataset.id
                    },
                    success: (result) => {
                        if (result) {
                            mui.toast('删除成功!')
                            getAddresses()
                        } else {
                            mui.alert('无法访问服务,请联系wx:13278865643');
                        }
                    }
                })
            })

            //加载点击的修改收货地址
            $('.updateGO').on('tap', function (e) {
                let addr = e.path[2].dataset
                if (addr && addr.id) {
                    setAddr(addr)
                    return
                }
                return
            })

        }
    })


}
function setAddr(addr) {
    let str = `
    <div>
        <input id='idUser' type="hidden"  placeholder="请输入收货人" value="${addr.id}">
    </div>
    <div class="mui-input-row">
        <input id='updateUser' type="text" class="mui-input-clear mui-input" placeholder="请输入收货人" value="${addr.recipients}">
    </div>
    <div class="mui-input-row">
        <input id='updateNumber' type="text" class="mui-input-clear mui-input" placeholder="请输入邮编" value="${addr.postcode}">
    </div>
    <div class="mui-input-row">
        <input id='showCityPicker'  type="text" class="mui-input-clear mui-input updateCityPicker"
            placeholder="请选择收货地址" value="${addr.address}">
    </div>
    <div class="mui-input-row">
        <input id='updateDetail' type="text" class="mui-input-clear mui-input" placeholder="详细地址"  value="${addr.addressdetail}">
    </div>
`
    $('#add_from').html(str)
}

//点击修改
$('#updateHandler').on('tap', (e) => {
    e.preventDefault()
    let id = $('#idUser').val()
    let address = $('.updateCityPicker').val()
    let addressDetail = $('#updateDetail').val()
    let recipients = $('#updateUser').val()
    let postcode = $('#updateNumber').val()
    $.ajax({
        type: 'POST',
        url: '/address/updateAddress',
        data: {
            id, address, addressDetail, recipients, postcode
        },
        success: (result) => {
            if (result.success) {
                mui.alert('修改成功!', () => {
                    viewApi.back();//返回上一个视图
                    getAddresses()
                    return
                });
            } else {
                mui.alert('无法访问服务,请联系wx:13278865643');
            }

        }
    })
})

//加载购物车
function uploadCar() {
    $.ajax({
        type: 'GET',
        url: '/cart/queryCart',
        success: (result) => {
            let strCar = ``
            let goods = result
            console.log(result)
            for (let g in goods) {
                strCar += `
                <li class="mui-table-view-cell">
                    <div class="mui-slider-handle">
                    <img class="mui-media-object mui-pull-left" src="${goods[g].pic[0].picAddr}">
                    <div class="mui-media-body">
                        <p class="mui-ellipsis">${goods[g].proName}</p>
                             <p><span class="span1">尺码:${goods[g].size}</span><span class="span1">数量:${goods[g].num}</span><span
                                    class="span1">价格:${parseInt(goods[g].price * goods[g].num)}元</span></p>
                        </div>
                    </div>
                    <div class="mui-slider-right mui-disabled">
                        <a id="delCar" class="mui-btn mui-btn-red" data-id="${goods[g].id}">删除</a>
                    </div>
                </li>
                `
            }
            $('#ul_his').html(strCar)
        }
    })
}

//点击删除某个商品
function delCar(id) {
    $.ajax({
        type: 'GET',
        url: '/cart/deleteCart',
        data: {
            id
        },
        success: (result) => {
            if (result) {
                mui.toast('删除成功!')
                uploadCar()
            } else {
                mui.alert('无法访问服务,请联系wx:13278865643');
            }
        }
    })
}

//清空购物车功能
function clearCar() {
    // $.ajax({
    //     type: '',
    //     url: '',
    //     success: (result) => {
    //         if (result) {
    //             mui.toast('已清空购物车!')
    //             uploadCar()
    //         } else {
    //             mui.alert('无法访问服务,请联系wx:13278865643');
    //         }
    //     }
    // })
    mui.alert('暂不支持该功能')
}